package cn.edu.nsmc.modules.sys.service.impl;

import cn.edu.nsmc.common.exception.RRException;
import cn.edu.nsmc.common.utils.Constant;
import cn.edu.nsmc.common.utils.R;
import cn.edu.nsmc.common.validator.ValidatorUtils;
import cn.edu.nsmc.common.validator.group.AddGroup;
import cn.edu.nsmc.modules.sys.dao.SysVisitorDao;
import cn.edu.nsmc.modules.sys.entity.SysUserEntity;
import cn.edu.nsmc.modules.sys.entity.SysVisitorEntity;
import cn.edu.nsmc.modules.sys.entity.data.FollowTheDetails;
import cn.edu.nsmc.modules.sys.entity.data.UserStatistics;
import cn.edu.nsmc.modules.sys.form.VisitorList;
import cn.edu.nsmc.modules.sys.form.req.ReqDepartmentForm;
import cn.edu.nsmc.modules.sys.form.req.ReqVisitorForm;
import cn.edu.nsmc.modules.sys.service.SysRoleService;
import cn.edu.nsmc.modules.sys.service.SysVisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author ZY
 * @Date 2024/2/20 19:08
 */
@Service("sysVisitorService")
public class SysVisitorServiceImpl extends ServiceImpl<SysVisitorDao, SysVisitorEntity> implements SysVisitorService {

    @Resource
    private SysVisitorDao sysVisitorDao;
    @Resource
    private SysRoleService sysRoleService;
    @Override
    public R addVisitor(SysVisitorEntity sysVisitorEntity) {
        //ValidatorUtils.validateEntity(sysVisitorEntity, AddGroup.class);
        sysVisitorEntity.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        if (StringUtils.isBlank(sysVisitorEntity.getPassword())) {
            sysVisitorEntity.setPassword("Cbyxy@269.pas");
        }
        sysVisitorEntity.setPassword(new Sha256Hash(sysVisitorEntity.getPassword(), salt).toHex());
        sysVisitorEntity.setSalt(salt);
        int i = sysVisitorDao.insert(sysVisitorEntity);
        //checkRole(sysVisitorEntity);
        return i == 1 ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R updateVisitor(SysVisitorEntity sysVisitorEntity) {
        int i = sysVisitorDao.updateVisitor(sysVisitorEntity);
        return i == 1 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteVisitorByIds(String[] ids) {
        for (String _id : ids) {
            Long aId = Long.valueOf(_id);
            Integer i = sysVisitorDao.deleteById(aId);
            if (i == 0) {
                return R.error("删除失败");
            }
        }
        return R.ok("删除成功");
    }

    @Override
    public R deleteVisitorById(Long id) {
        Integer i = sysVisitorDao.deleteById(id);
        if (i == 0){
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }

    @Override
    public R getVisitorList(VisitorList visitorList) {
        visitorList.setPage((visitorList.getPage() - 1) * visitorList.getSize());
        List<ReqVisitorForm> list = sysVisitorDao.getDepartmentList(visitorList);
        Integer total = sysVisitorDao.getDepartmentListNumber(visitorList);
        R r =  R.page("查询成功", list, total);
        return r;
    }

    @Override
    public R getVisitorById(Long id) {
        ReqVisitorForm reqVisitorForm = sysVisitorDao.getVisitorById(id);
        return reqVisitorForm != null ? R.success("查询成功", reqVisitorForm) : R.error("查询失败");
    }

    @Override
    public R getInformation() {
        FollowTheDetails followTheDetails = new FollowTheDetails();
        followTheDetails.setTotalNumberOfPeople(sysVisitorDao.getTotalNumberOfPeople());
        followTheDetails.setNumberOfConcerns(sysVisitorDao.getNumberOfConcerns());
        followTheDetails.setPercentage(followTheDetails.getNumberOfConcerns()/ followTheDetails.getTotalNumberOfPeople());
        return R.success("查询成功",followTheDetails);
    }

    @Override
    public R getTypeNumber(UserStatistics userStatistics) {
        userStatistics = sysVisitorDao.getTypeNumber(userStatistics);
        return R.success("ok",userStatistics);
    }

}
